﻿using Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Git
{
    partial class git
    {

        private Boolean gitcheckouttest = false;
        void p_gitcheckout_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                if (Regex.IsMatch(e.Data, "Your branch is up-to-date with"))
                {
                    gitcheckouttest = true;
                }
                if (Regex.IsMatch(e.Data, "Branch.*?set up to track remote branch.*?from origin"))
                {
                    gitcheckouttest = true;
                }
                Console.WriteLine(e.Data);
            }
        }

        public Boolean gitcheckout(string file, string branch)
        {
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                p.OutputDataReceived += p_gitcheckout_OutputDataReceived;
                p.Start();
                p.StandardInput.WriteLine(@"cd """ + file + @"""");
                p.StandardInput.WriteLine("git checkout " + branch);
                //p.StandardInput.WriteLine("git pull");
                p.StandardInput.WriteLine("exit");
                p.BeginOutputReadLine();
                p.WaitForExit();
                p.Close();
                return gitcheckouttest;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
